package com.kedou.net;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.kedou.app.Config;
import com.kedou.app.R;
import com.kedou.domain.User;
import com.kedou.net.PostUserInfo.FailCallback;
import com.kedou.net.PostUserInfo.SuccessCallback;
import com.kedou.util.Util;

/**
 * 后台交互：获取我的财富
 * 
 * @author zhouchao
 * 
 */
public class PostUserWealth {
	public PostUserWealth(String token, String userId, String os,
			String deviceId, final SuccessCallback successCallback,
			final FailCallback failCallback, final Context context) {
		new NetConnection(
				Util.getSDBaseUrl() + Config.URL_GETUSERWEALTH,
				HttpMethod.POST,
				new NetConnection.SuccessCallback() {

					@Override
					public void onSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							JSONObject dataObject = obj
									.getJSONObject(Config.KEY_DATA);
							switch (obj.getInt(Config.KEY_STATUSCODE)) {
							case Config.RESULT_STATUS_SUCCESS:
								String mProjectValuation = null;
								String mTotalFund = null;
								if (dataObject.has("projectValuation")) {
									mProjectValuation = dataObject
											.getString("projectValuation");
								}
								if (dataObject.has("totalFund")) {
									mTotalFund = dataObject
											.getString("totalFund");
								}
								if (successCallback != null) {
									successCallback.onSuccess(
											obj.getString(Config.KEY_STATUSMSG),
											mProjectValuation, mTotalFund);
								}
								break;
							default:
								if (failCallback != null) {
									failCallback.onFail(obj
											.getString(Config.KEY_STATUSMSG));
								}
								break;
							}

						} catch (JSONException e) {
							e.printStackTrace();
							if (failCallback != null) {
								failCallback.onFail(context.getResources()
										.getString(R.string.server_have_wrong));
							}
						}
					}
				}, new NetConnection.FailCallback() {

					@Override
					public void onFail() {
						if (failCallback != null) {
							failCallback.onFail(context.getResources()
									.getString(R.string.server_have_wrong));
						}
					}
				}, Config.KEY_TOKEN, token, Config.KEY_USERID, userId,
				Config.KEY_OS, os, Config.KEY_DEVICE, deviceId);
	}

	public static interface SuccessCallback {
		void onSuccess(String msg, String projectValuation, String totalMoney);
	}

	public static interface FailCallback {
		void onFail(String msg);
	}
}
